﻿using System;
using System.Data.Metadata.Edm;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Data.Objects.DataClasses;

namespace MediaLibrary.Test
{
    /// <summary>
    /// Summary description for UnitTest1
    /// </summary>
    [TestClass]
    public class UnitTest1
    {
        private TestContext _testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return _testContextInstance;
            }
            set
            {
                _testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        [TestMethod]
        public void TestMethod1()
        {
            var fourccId = Guid.NewGuid();
            var cs = @"metadata=res://*/MediaLibraryModel.csdl|res://*/MediaLibraryModel.ssdl|res://*/MediaLibraryModel.msl;provider=System.Data.SqlServerCe.3.5;provider connection string=""Data Source=|DataDirectory|\..\..\..\DataBase\MyDatabase#1.sdf""";
            MediaLibraryModelContainer mlmc = new MediaLibraryModelContainer(cs);
            var ec = new EntityCollection<MetaData>();
            var md = new MetaData { Key = "FourCC", Value = "DIVX", Id = fourccId};
            ec.Add(md);
            md = new MetaData { Key = "Duration", Value = "05:00", Id = Guid.NewGuid() };
            ec.Add(md);
            var fileId = Guid.NewGuid();
            var entity = new File {MetaData = ec, Id = fileId , Path=@"C:\perios.avi"};
            
            mlmc.EntitySet.AddObject(entity);
            mlmc.SaveChanges();

            // NOT THE WAY TO GO !

            var check = (from es in mlmc.EntitySet.OfType<File>().Include("MetaData").Where(o => o.Id == fileId)
                         select es.MetaData).First().Where(o => o.Key == "FourCC").Select((o,e)=>o.Value); 
            //var v2 = check.First().Where(o => o.Key == "FourCC");
            //var v2 = check.Where(o => o.Any(n => n.Key == "FourCC"));
            //var v = check.Where((o => o.First(n=>n.Id == fourccId)));
            //var v = check.Where(z => z.MetaData.Where(o => o.Id == fourccId));
            Assert.IsTrue(check.Contains("DIVX"));
        }
    }
}
